package spider

import (
	"fmt"
	"io/ioutil"
	"os"
	"time"

	"github.com/franela/goreq"
)

// FetchJSON 获取
type FetchJSON struct {
	PageCount int
}

// Fetch 获取
func (c *FetchJSON) Fetch() error {
	for i := 115; i <= c.PageCount; i++ {
		url := fmt.Sprintf("http://mg.cloud.chargerlink.com/api/commercial/findCommercials?_index=%d", i)
		req := goreq.Request{
			Uri:         url,
			Method:      "GET",
			ContentType: "application/json;charset=UTF-8",
			Host:        "mg.cloud.chargerlink.com",
		}
		req.AddHeader("Refer", "http://mg.cloud.chargerlink.com/")
		req.AddHeader("token", "d0fef659c4f2497d8fef88b0870863aa")
		req.AddHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36")
		req.AddHeader("X-Requested-With", "XMLHttpRequest")
		resp, err := req.Do()
		if err != nil {
			fmt.Println("req err:", url)
			time.Sleep(time.Duration(2) * time.Second)
			continue
		}
		str, err := resp.Body.ToString()
		if err != nil {
			fmt.Println("body err:", url)
			time.Sleep(time.Duration(2) * time.Second)
			continue
		}
		fileName := fmt.Sprintf("json/%d.txt", i)
		ioutil.WriteFile(fileName, []byte(str), os.ModePerm)
		time.Sleep(time.Duration(1) * time.Second)
	}
	return nil
}
